<template>
    <div class="loginPage">
        <el-card class="content">
            <el-form ref="loginFormRef" :model="loginForm" status-icon :rules="loginRules" label-width="120px"
                class="demo-ruleForm">
               <div class="loginTitle"><h1>电商管理系统</h1></div>
                <el-form-item label="账号" prop="username">
                    <el-input v-model="loginForm.username" type="text" autocomplete="off" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="loginForm.password" type="password" autocomplete="off" />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm(loginFormRef)">提交</el-button>
                    <el-button @click="resetForm(loginFormRef)">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { FormInstance } from 'element-plus'
import { reactive, ref } from 'vue'
import { useStore } from '@/store'
import * as login from '@/store/modules/admin/actionsTypes'
import router from '@/router'
import { useRoute } from 'vue-router'
const store = useStore()
const route = useRoute()
const loginFormRef = ref<FormInstance>()

const loginForm = reactive({
  username: '',
  password: ''
})
const validateUsername = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入账号'))
    console.log('validateUsername')
  }
  callback()
}
const validatePassWord = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  }
  callback()
}

const loginRules = reactive({
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [{ validator: validatePassWord, trigger: 'blur' }]
})

const submitForm = (loginFormRef:FormInstance | undefined) => {
  // if (loginFormRef) {
  //   console.log(loginFormRef)
  //   loginFormRef.validate((valid) => {
  //     console.log(valid, 'valid')
  //     if (valid) {
  //       store.dispatch(`adminStore/${login.LOGIN}`, loginForm)
  //     } else {
  //       return false
  //     }
  //   })
  // } else {
  //   return false
  // }

  if (!loginFormRef) return false
  loginFormRef.validate(async (valid) => {
    if (valid) {
      await store.dispatch(`adminStore/${login.LOGIN}`, loginForm)
      let redirect = route.query.redirect || '/'
      if (typeof (redirect) !== 'string') {
        redirect = '/'
      }
      console.log(redirect, 'login_redirect')
      router.replace(redirect)
    }
  })
}
const resetForm = (loginFormRef:FormInstance | undefined) => {
  if (!loginFormRef) {
    return false
  }
  loginFormRef.resetFields()
  //   console.log(loginFormRef, 'loginFormRef')
  loginForm.username = 'admin'
  loginForm.password = '123456'
}

</script>

<style lang="scss" scoped>
.loginPage {
    display: flex;
    height: 100vh;
    align-items: center;
    justify-content: center;
    flex-direction: column;

    .el-card {
        width: 400px;
        height: 280px;
        display: flex;
        text-align: center;
        justify-content: center;
        .el-form-item{
            margin-left: -100px;
        }
    }
}
</style>
